using System;
using System.Collections.Generic;
using System.Text;

using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace SPRSS.WebParts
{
    public class RSSRenderEdit : EditorPart
    {
        private TextBox rssFeedUrl;
        private TextBox xsltText;

        public RSSRenderEdit()
        {
            this.ID = "RSSEditor";
            this.Title = "Editor for RSS Render WebPart";
        }

        protected override void CreateChildControls()
        {
            Label rssLabel = new Label();
            rssLabel.Text = "Rss Feed URL:";
            rssLabel.Style.Add(HtmlTextWriterStyle.FontFamily, "Tahoma, Arial, Verdana");
            rssLabel.Style.Add(HtmlTextWriterStyle.FontSize, "10pt");
            rssLabel.Style.Add(HtmlTextWriterStyle.FontWeight, "Bold");
            Controls.Add(rssLabel);
            Controls.Add(new LiteralControl("<br/>"));

            rssFeedUrl = new TextBox();
            rssFeedUrl.TextMode = TextBoxMode.MultiLine;
            rssFeedUrl.Width = new Unit("200px");
            rssFeedUrl.Height = new Unit("200px");
            Controls.Add(rssFeedUrl);
            Controls.Add(new LiteralControl("<br/>"));

            Label xsltLabel = new Label();
            xsltLabel.Text = "XSLT Transform:";
            xsltLabel.Style.Add(HtmlTextWriterStyle.FontFamily, "Tahoma, Arial, Verdana");
            xsltLabel.Style.Add(HtmlTextWriterStyle.FontWeight, "Bold");
            xsltLabel.Style.Add(HtmlTextWriterStyle.FontSize, "10pt");
            Controls.Add(xsltLabel);
            Controls.Add(new LiteralControl("<br/>"));

            xsltText = new TextBox();
            xsltText.TextMode = TextBoxMode.MultiLine;
            xsltText.Width = new Unit("200px");
            xsltText.Height = new Unit("200px");
            Controls.Add(xsltText);
            Controls.Add(new LiteralControl("<br/>"));

            base.CreateChildControls();
        }

        public override bool ApplyChanges()
        {
            EnsureChildControls();
            RSSRender part = WebPartToEdit as RSSRender;

            if (part != null)
            {
                part.RssUrl = rssFeedUrl.Text;
                part.XSLTransform = xsltText.Text;
                return true;
            }
            else
            {
                return false;
            }
        }

        public override void SyncChanges()
        {
            EnsureChildControls();
            RSSRender part = WebPartToEdit as RSSRender;

            if (part != null)
            {
                rssFeedUrl.Text = part.RssUrl;
                xsltText.Text = part.XSLTransform;
            }
        }
    }
}